<?php

class Community_RatecourseController extends Zend_Controller_Action
{
	protected $cid, $sem, $year, $user, $courseNo, $school, $dept;
	 protected $_flashMessenger = null;
    public function init()
    {
			$this->cid = $this->_getParam('course');
      		$this->sem = $this->_getParam('semester');
      		$this->year = $this->_getParam('year');
      		$this->courseNo = $this->_getParam('course_no');
      		$this->school = $this->_getParam('school');
      		$this->dept = $this->_getParam('depts');
      		
      		
      		//If the user is authenticated (need to perform an action requiring authentication)
      		//Get his identity.
      		if (Zend_Auth::getInstance()->hasIdentity()){
      			$this->user =  Zend_Auth::getInstance()->getIdentity()->id;
      		}
       
    }

    public function indexAction()
    {
    	$this->view->headTitle("Please select your school:", 'PREPEND');
    	
        // action body
        $recs = new Community_Model_DbTable_coursedetails();
        $list = $recs->getDistinctSchools();
        //print_r($list);
       //Pagination control on the page
        $paginator = new Zend_Paginator(new Zend_Paginator_Adapter_DbSelect($list));
        $paginator->setItemCountPerPage(5)
        		  ->setCurrentPageNumber($this->_getParam('page', 1));
       
      $this->view->paginator = $paginator;
      
    }

    /**
     * Function: getperiod
     * This function gets the particular period a course was taken for rating purposes
     */
    public function getperiodAction(){
        $type = $this->_getParam('type');
        
        if ($type == 'rate'){
        	
        	$this->periodHelper('rate');
        }
     if ($type == 'viewavg'){
    		
    		$this->periodHelper('viewaverage');
    	}
     if ($type == 'viewcomments'){
     	
     	$this->periodHelper('viewcomments');
     }
     
     if ($type == 'diff'){
     	$this->periodHelper('diff');
     }
     
     if ($type == 'instr'){
     	$this->periodHelper('instr');
     }
     
     if ($type == 'text'){
     	$this->periodHelper('text');
     }
    
     if ($type == 'grade'){
     	$this->periodHelper('grade');
     }
    
     if ($type == 'time'){
     	$this->periodHelper('time');
     }
    }
    
    
    public function deletecommentAction() {
    	
    	$id = $this->_getParam('id');
    	$comments = new Community_Model_ratecourse();
    	$comments->delRating($id);
    	$request = new Zend_Controller_Request_Http();
    	$url = $request->getRequestUri();
    	$this->view->message="This comments and all associated ratings have been deleted!";
    }
    /**
     * Function: rate
     * This method allows a user rate a particular course
     */
     public function rateAction() {
     	
     	   $this->view->headTitle("Rate this course now", 'PREPEND');
     	
     	   // $cid = $this->getRequest()->getPost('course');
     	   			$this->view->data = $this->courseNo . " ( " .$this->sem . " " .$this->year . " )";
     	
     	   $rating = new Community_Model_ratecourse();
     	   $recs =$rating->checkRecord($this->cid, $this->user, $this->sem, $this->year); 
     	   //If the count is less than 1
     	   //Then no records for the user rating this course exists, so the 
     	   //User can proceed with rating this particular course
     	   $count = $recs[0]['val'];
           if ($count < 1){
        	$rate = new Community_Form_RateClass();
        	
        	$this->view->form = $rate;
       	
        	/**
        	 * This gets all the information necessary to rate a 
        	 * Particular course.
        	 */
       	if ($this->getRequest()->isPost()){	
       		$data = $this->getRequest()->getParams();
       		$timespent = $data['time_spent'];
       		$difficulty = $data['difficulty'];
       		$finalgrade = $data['final_grade'];
       		$instructor = $data['instructor_rating'];
       		$text = $data['usefulness_text'];
       		$comments = $data['comments']['Editor'];
       		$rating->addRating($difficulty, $text, $instructor, $timespent, $finalgrade, $comments, $this->user, $this->cid, $this->year, $this->sem);
       		$db = new Account_Model_DbTable_myratings();
       		$db->addRatings($this->courseNo, $this->sem, $this->year, $this->user);
       		
       		$this->_forward('thank', 'ratecourse', 'community', array('no'=>$this->courseNo, 'sem'=>$this->sem, 'year'=>$this->year, 'school'=>$this->school, 'dept'=>$this->dept));
       		}
           }else{
           	$this->view->form = "Our records indicate that you have already rated this instance of the course";
           }
          
        }
        	
        /**
         * Thanks Message after rating course
         */
    	public function thankAction(){
    		
    	   $this->view->headTitle("We appreciate your rating", 'PREPEND');
    		
    		
    	  $this->view->message = "Thanks for rating " . $this->_getParam('no') . " for the" ." " .$this->sem . " " .$this->year . " semeter"; 	
    	  $this->view->school = $this->school;
    	  $this->view->dept = $this->dept;
    	  
    	}
       /**
        * Function: ViewRatings
        * This function returns the ratings of particular course
        */
		
      public function viewcommentsAction() {
      	    	$this->view->headTitle("View Comments", 'PREPEND');
      	
      	$rat = new Community_Model_ratecourse();
      	$comments = $rat->viewComments($this->cid, $this->sem, $this->year);
      	$this->pagination($comments, 7);
      				$this->view->data = $this->courseNo . " ( " .$this->sem . " " .$this->year . " )";
      	
      }
 
      /**
       * Function: viewaverage
       * This function returns the average ratings of a particular course
       */
      public function viewaverageAction() {
      	    	$this->view->headTitle("View Course Average", 'PREPEND');
      	
		$this->setVars();      	
      	$rating = new Community_Model_ratecourse();  
      	$avg = $rating->avgRatings($this->cid, $this->sem, $this->year);
      	$this->pagination($avg, 1);
      	$this->view->name = $this->courseNo ." (" .$this->sem ." " .$this->year .")";
      }
		public function getdeptsAction() {
			$this->view->headTitle("Choose your department", 'PREPEND');
			
			$id = $this->_getParam('school');
			$depts = new Community_Model_DbTable_coursedetails();
			$results = $depts->getschooldepts($id);
			$this->pagination($results, 10);
		}
		
		public function getcoursesAction() {
		$this->view->headTitle("Choose your course", 'PREPEND');
			
		$id = $this->_getParam('id');
		$sid = $this->_getParam('school');
		$courses = new Community_Model_DbTable_coursedetails();
		$results = $courses->getcourses($id, $sid);
		$this->pagination($results, 5);
		}
	
		/**
		 * Pagination: Helper function
		 * @param unknown_type $resultset
		 * @param unknown_type $count
		 */
		private function pagination($resultset, $count){
        $paginator = new Zend_Paginator(new Zend_Paginator_Adapter_DbSelect($resultset));
        $paginator->setItemCountPerPage($count)	        
        		  ->setCurrentPageNumber($this->_getParam('page', 1));
        $this->view->paginator = $paginator;
		}
		
		/**
		 * Function: periodHelper
		 * This function allows the getPeriodAction decide what action to generate
		 * upon the selection of a course option
		 * @param unknown_type $action
		 */
		private function periodHelper($act){
    	$form = new Community_Form_time();
    	
    	$form->setMethod('get');
    	$form->setAction($this->getFrontController()->getBaseUrl() . '/community/ratecourse/' .$act);
    	$this->_setParam('add', 1);
    	
    	/* $this->_helper->url(array(
    	'module' => 'admin',
    	'controller'=> 'ratecourse',
    	'action' => 'rate')));*/
    	$this->view->form = $form;
		}
		
		/**
		 * Function: DiffAction
		 * This method gets the distribution of the difficulty among all respondents who voted on a course
		 */
		public function diffAction(){
			$this->view->headTitle("Difficulty distribution for this course", 'PREPEND');
			
			$this->setVars();
			
			$count = 0;
			$num = 1;
			$values = array();
			$data = new Community_Model_ratecourse();
			for ($a = 1; $a < 6; $a ++){
			$arr = $data->getDistribution($this->cid, $this->sem, $this->year, $a);
			array_push($values, $arr);
			}
			
			//Now calculate the total
			for ($a = 0; $a < 5; $a++){
				$count += $values[$a];
			}
			//To prevent a division by zero error, assign count the value of 1
			if ($count == 0){
				$count = 1;
				$num = 0;
			}
			$this->view->num = $num;
			$this->view->count = $count;
			$this->view->pos = $values;
			$this->view->data = $this->courseNo . " ( " .$this->sem . " " .$this->year . " )";
		}
		
		/**
		 * Function: Instructor Rating
		 * This method gives a distribution of how respondents felt about the instructor of the course
		 */
		public function instrAction(){
			$this->view->headTitle("Instructor distribution for this course", 'PREPEND');
			
			$this->setVars();
						
			$count = 0;
			$num = 1;
			$values = array();
			$data = new Community_Model_ratecourse();
			for ($a = 1; $a < 6; $a ++){
			$arr = $data->getInstructorDistribution($this->cid, $this->sem, $this->year, $a);
			array_push($values, $arr);
			}
			
			//Now calculate the total
			for ($a = 0; $a < 5; $a++){
				$count += $values[$a];
			}
			//To prevent a division by zero error, assign count the value of 1
			if ($count == 0){
				$count = 1;
				$num = 0;
			}
			$this->view->num = $num;
			$this->view->count = $count;
			$this->view->pos = $values;
		}
		
		/**
		 * Function: TextAction
		 * Returns a distribution of the text as rated by the various users
		 */
		public function textAction() {
			$this->view->headTitle("Text distribution for this course", 'PREPEND');
			
			$this->setVars();
						
			$count = 0;
			$num=1;
			$values = array();
			$data = new Community_Model_ratecourse();
			for ($a = 1; $a < 6; $a ++){
			$arr = $data->getTextDistribution($this->cid, $this->sem, $this->year, $a);
			array_push($values, $arr);
			}
			
			//Now calculate the total
			for ($a = 0; $a < 5; $a++){
				$count += $values[$a];
			}
			//To prevent a division by zero error, assign count the value of 1
			if ($count == 0){
				$count = 1;
				$num = 0;
			}
			$this->view->num = $num;
			$this->view->count = $count;
			$this->view->pos = $values;
			
		}
		/*
		 * Function: gradeAction
		 * This function returns a distribution of the grades
		 */
		public function gradeAction() {
			$this->view->headTitle("Grade distribution for this course", 'PREPEND');
			
			$this->setVars();
			
			$count = 0;
			$num = 1;
			$values = array();
			$data = new Community_Model_ratecourse();
			for ($a = 1; $a < 6; $a ++){
			$arr = $data->getGradeDistribution($this->cid, $this->sem, $this->year, $a);
			array_push($values, $arr);
			}
			
			//Now calculate the total
			for ($a = 0; $a < 5; $a++){
				$count += $values[$a];
			}
			//To prevent a division by zero error, assign count the value of 1
			if ($count == 0){
				$count = 1;
				$num =0;
			}
			$this->view->num = $num;
			$this->view->count = $count;
			$this->view->pos = $values;
			
		}
		
		/**
		 * Function: timeAction
		 * This function returns a distribution of the time Burden of an instance of this course 
		 * according to the ratings of respondents
		 */

		public function timeAction() {
			$this->view->headTitle("Time distribution for this course", 'PREPEND');
			
			$this->view->data = $this->courseNo . " ( " .$this->sem . " " .$this->year . " )";
			$this->setVars();
			$count = 0;
			$num = 1;
			$values = array();
			$data = new Community_Model_ratecourse();
			for ($a = 1; $a < 6; $a ++){
			$arr = $data->getTimeDistribution($this->cid, $this->sem, $this->year, $a);
			array_push($values, $arr);
			}
			
			//Now calculate the total
			for ($a = 0; $a < 5; $a++){
				$count += $values[$a];
			}
			//To prevent a division by zero error, assign count the value of 1
			if ($count == 0){
				$count = 1;
				$num = 0;
			}
			$this->view->num = $num;
			$this->view->count = $count;
			$this->view->pos = $values;
			
		}
		
		/**
		 * This function sets variables to be used in the view
		 */
		private function setVars(){
			$this->view->data = $this->courseNo . " ( " .$this->sem . " " .$this->year . " )";
			$this->view->course = $this->cid;
			$this->view->year = $this->year;
			$this->view->sem = $this->sem;
			$this->view->courseNo = $this->courseNo;
			
		}
}
